home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 415_02 / rtti / rtti.README < prev   
Encoding:
Text File  |  1993-09-25  |  10.7 KB  |  320 lines

  1. RTTI  (Run-time Type information for C++)
  2. ------------------------------------------
  3. Author - Arindam Banerji    (axb@cse.nd.edu)
  4.      384 FitzPatrick Hall
  5.      Dept. of Computer Science & Engineering
  6.      University of Notre Dame
  7.      Notre Dame, IN 46556
  8.  
  9. Please report any problems to axb@cse.nd.edu. I make no guarantees about
  10. this software.
  11.  
  12. This directory contains the sources and appropriate binaries for an
  13. extensible C++ run-time type system. This system is loosely based on the
  14. RTTI system demonstrated by Stousroup in "The C++ Programming Language".
  15.  
  16. The files in this directory are
  17.  
  18. rtti.README      - this file
  19.  
  20. rtti_make_vars   - File containing the defintiions of some of the make
  21.            make file variables used in building the rtti libraries.
  22.  
  23. The file in the include directory are
  24. String.h   - description of a simple String class. The name of the class is
  25.          "string". In case, this conflicts with any of the other class
  26.          names that you are using, then enclose the class declaration
  27.          within something like
  28.          #define   string    __xx_string
  29.          #undef    string
  30.  
  31. trace.h   - a simple trace class that helps in debugging. The name of the
  32.         class is "trace". In case, this conflicts with any other class
  33.         names that you are using, then enclose the class declaration
  34.         within something like
  35.          #define   trace    __xx_trace
  36.          #undef    trace
  37.  
  38.  
  39. rtti_entry.h -  The main include file for the rtti classes. This file pulls
  40.         in the other include files (hence, this is the file that
  41.         user code pulls in). In addition, it defines the classes
  42.         that allow for the rtti implmentation to be bootstrapped.
  43.  
  44. rtti.h   -    This file declares the class definitions for the rtti type
  45.           system (ie: all the classes that are actually visible to the
  46.           user are declared here).
  47.  
  48. rttiimpl.h - This file declares some of the classes used to implment the
  49.          manipulation and maintenance of base class lists, in the rtti
  50.          type system.
  51.  
  52. rttimacros.h - Most users will just use this for building the rtti
  53.            scaffoldings into their class declarations and defitions.
  54.            The assumption here is that the declarations and definitions
  55.            are in two separate files ie: for a class such as foo, there
  56.            are two files foo.c and foo.h. Obviously, each file may
  57.            contain more than one class declaration and definition (foo.c
  58.            and foo.h may define and declare the classes foo, bar, Lists,
  59.            etc.)
  60.  
  61.  
  62. The files in the src directory are
  63. String.c      - A simple string class definition. The name of this class
  64.         is string. In case, this name conflicts with other classes
  65.         that you are using, then enclose the member functions
  66.         within something like
  67.         #define   string    __xx_string
  68.         #undef    string
  69.  
  70.  
  71. trace.c   - Member function definitions for the trace class. The name of the
  72.         class is "trace". In case, this conflicts with any other class
  73.         names that you are using, then enclose the class declaration
  74.         within something like
  75.          #define   trace    __xx_trace
  76.          #undef    trace
  77.  
  78. rtti_entry.c - This file contains the defintion of the hndl_recusrsion class
  79.            that allows the type system to be bootstrapped.
  80.  
  81.  
  82. rtti.c - This file contains the implmentation of the rtti classes.
  83.  
  84. Implementation Notes :
  85. ----------------------
  86.  
  87. All classes in this RTTI system inherit from the CLASS base class. This
  88. allows public virtual inheritance to work. CLASS has very little
  89. functionality associated with it, although it itself has the RTTI
  90. scaffoldings neccessary for all users of RTTI. The narrowing facility
  91. (ie: going from a base class pointer to a derived class pointer) depends
  92. upon the use of CLASS.
  93.  
  94. The Type_info class is the core of the RTTI implementation. It is
  95. initialized once, per class. The constructors accept a list of the base
  96. classes and the name of the class. The typeid class provides an interface
  97. to the RTTI system and users get at the RTTI thru this class. This class
  98. just acts like a pass thru for the Type_info class.
  99.  
  100. The base_iterator class is initialized with the list of base classes. It
  101. allows the clients of the Type_info class to iterate thru the list of bases.
  102.  
  103. The other classes such as trace and string are fairly trivial.
  104.  
  105. Usage :
  106. --------
  107.  
  108. Every class that uses the RTTI must use some scaffolding in both the
  109. declaration and the definition of the class. The declaration must
  110. contain the following:
  111.  
  112. For every RTTI class, a static Type_info class needs to be created, as in
  113. static  const Type_info info_obj ;
  114.  
  115. For every RTTI class, two functions must be created to retrieve the Type
  116. information (one a virtual function and another a simple function), as in
  117.    virtual typeid get_info() const ;
  118.    static  typeid  info() ;
  119.  
  120. In order to allow a base class pointer to be converted into its appropriate
  121. derived class pointer, the narrowing facility is provided.
  122. static  Type_info  *_narrow(CLASS *) ;
  123.  
  124. In order for the narrowing facility to work in the presence of virtual
  125. public inheritance, every RTTI class defines a virtual function that
  126. returns its this pointer.
  127. virtual void *get_this_ptr(void) const ;
  128.  
  129.  
  130. However, the macros defined in rttimacros.h make it almost trivial to
  131. add support for this in the class declarations. The user has to add just
  132. a simple line that automatically generates all the above-mentioned
  133. declarations.
  134.  
  135. Example 1
  136. Consider a class foo, of the form
  137. class   foo : public virtual CLASS
  138.    {
  139.      public :
  140.        foo() ;
  141.        ~foo() ;
  142.       .....
  143.    } ;
  144.  
  145. In order to create scaffolding for this, add the following line to the
  146. class declaration (ie: in the .h file) as in :
  147. class   foo : public virtual CLASS
  148.    {
  149.      public :
  150.        foo() ;
  151.        ~foo() ;
  152.        RTTI_SCAFFOLDING_DECL(foo)
  153.       .....
  154.    } ;  // some further additions need to be made in the .c file, too
  155.  
  156. Example 2
  157. If instead of the simplistic foo, we had
  158. template <class T>
  159.   class  foo1 : public LinkedList<T> { ...
  160.  
  161. add the following line
  162.     RTTI_SCAFFOLDING_DECL_TEMPL(foo1)
  163.  
  164. Example 3
  165. In case things are a little more complicated, as in
  166. template <class T, int n >
  167.   class  foo2  { ...
  168.  
  169. add the following line
  170.     RTTI_SCAFFOLDING_DECL_TEMPL_2(foo2,T,n)
  171.  
  172. Example 4
  173. For the following
  174. template <class T, int n, class Z >
  175.   class  foo3 : public some_parent<T,n>  { ...
  176. add the following line
  177.   RTTI_SCAFFOLDING_DECL_TEMPL_3(foo3,T,n,Z)
  178.  
  179.  
  180. The class definition in the .c or .C files must include a corresponding
  181. piece of scaffolding. The scaffolding is as follows :
  182.  
  183. The set of direct bases for a particular RTTI class need to be listed and
  184. passed as a parameter to the Type_info object for the class. For a class
  185. called TYPE with just one parent class (say PARENT1), the following could
  186. be used.
  187.    static const Type_info*  TYPE_set_bases[] =
  188.            {(const Type_info *) &PARENT1::info_obj, 0 } ;
  189.  
  190. The Type_info object for the class Type mentioned above needs to be
  191. instantiated. One parameter is the name TYPE and the other is the list of
  192. base classes declared above.
  193.   const  Type_info TYPE::info_obj("TYPE", TYPE_set_bases) ;
  194.  
  195. The two functions that allow for the retrival of the Type_info classes
  196. from the RTTI class need to be defined (as show below).
  197.   typeid TYPE::get_info() const { return &info_obj; }
  198.   typeid TYPE::info() { return &info_obj; }
  199.  
  200. The narrowing mechanism, is an extremely useful part of the RTTI interface.
  201. Often base class pointers to derived class objects are much easier to pass
  202. around (especially where a strict separation between interface and
  203. implementation is desired.) However, during the actual use of such a pointer
  204. it is necessary to retrieve the actual derived object type. Thus, a combina-
  205. -tion of the Type information of the object and the narrowing mechanism
  206. may be used to get back the original pointer. The following two functions
  207. allow for this to happen.
  208. TYPE    *TYPE::_narrow(CLASS *obj)
  209.   {
  210.        void   *ret =  NULL ;
  211.      if ( ptr_cast(TYPE,obj))
  212.          ret = obj->get_this_ptr() ;
  213.       return ( (TYPE *) ret) ;
  214.    }
  215.  
  216. void *TYPE::get_this_ptr(void) const
  217.   {
  218.     return ((void *) this);
  219.   }
  220.  
  221.  
  222. Again, the macros defined in rttimacros.h make it almost trivial to
  223. add support for this in the class definitions. The user has to add just
  224. a simple line that automatically generates all the above-mentioned
  225. member functions and data declarations.
  226.  
  227. Example 1
  228. For a  class foo, of the form
  229. class   foo : public virtual CLASS
  230.    {
  231.      public :
  232.        foo() ;
  233.        ~foo() ;
  234.        RTTI_SCAFFOLDING_DECL(foo)
  235.       .....
  236.    } ;
  237.  
  238. In order to create scaffolding for this, add the following line to the
  239. class definition (ie: in the .c file) as in :
  240.    RTTI_SCAFFOLDING_IMPL1(foo, CLASS)
  241.  
  242. Similiarly, if the class foo had no parents use the macro
  243.     RTTI_SCAFFOLDING_IMPL0(foo)
  244. and if it had two parents, use the macro
  245.     RTTI_SCAFFOLDING_IMPL2(foo,CLASS,PARENT2)
  246. and so on.
  247.  
  248. Example 2
  249. If instead of the simplistic foo, we had
  250. template <class T>
  251.   class  foo1 { ...
  252.  
  253. add the following line to the definition file
  254.    TEMPL_RTTI_IMPL_SCAFF_0_1(foo1,foo1<T>,class T)
  255. If foo1 instead had the parameters <classT, int n> we could use
  256.    TEMPL_RTTI_IMPL_SCAFF_0_2(foo1,class T,int n,T,n)
  257. and so on.
  258.  
  259. Example 3
  260. For the following
  261. template <class T, int n, class Z >
  262.   class  foo3 : public some_parent<T,n>  { ...
  263. add the following line
  264. TEMPL_RTTI_IMPL_SCAFF_1_3(foo3,class T,int n,class Z,some_parent<T,n>,T,n,Z)
  265.  
  266. As you might have noticed the the format of the macro name is
  267.   TEMPL_RTTI_IMPL_SCAFF_numparents_numparms (Please see rttimacros.h )
  268.  
  269. User interface
  270. ---------------
  271. 1.
  272. The user may retieve the static type information of an object thru the
  273. the following macro
  274.     static_type_info(T)
  275. where T is the Type name.
  276.  
  277. 2.
  278. To retrieve the Type information for a given pointer , use
  279.   ptr_type_info(p)
  280. where p is the appropriate pointer.
  281.  
  282. 3.
  283. To retrieve the Type information for a given reference , use
  284.   ref_type_info(r)
  285. where p is the appropriate reference.
  286.  
  287. 4.
  288. To check whether a pointer may be cast to particular type, use any of the
  289. following macros (returns NULL, is invalid)
  290.   ptr_cast(T,p) , where T is the Type and p the pointer.
  291. as in
  292.   template <class T>
  293.      class    foo : public virtual CLASS
  294.  use the expression
  295.      ptr_cast(Type_to_be_cast_to, ptr_to_foo_object)
  296.  
  297. Similiarly, to compare a pointer p with the following
  298.   template <class T, int n , class Z>
  299.      class    foo2 : public some_parent<T,n, Z>
  300. use
  301.   ptr_cast_3(foo2,T,n,Z,p)
  302.  
  303.  
  304. 5.
  305. To narrow a pointer p to a base foo2 (as shown in 4. above)
  306.   use foo2<T,n,Z>::_narrow(p)
  307.  
  308.  
  309.  
  310. Building
  311. ---------
  312. The Makefile shown in bin pulls in the include file rtti_make_vars. This
  313. file contains the defition of the variables that'll most probably need to
  314. changed, during the compilation such as path name of the C++ compiler, etc.
  315.  
  316. I have used xlC and cfront 3.0.1 (on the RS/6000s) to build these sources
  317. and have not had any problems. Since, template implmentations on different
  318. compilers are quite different, you might need to tweak the macros somewhat
  319. to make your compiler happy.
  320.